<?php

namespace App\Http\Controllers\Goods\Order;

use App\Http\Controllers\Controller;
use App\Http\Requests\Goods\Order\AddCartRequest;
use Illuminate\Http\Request;
use App\Models\Goods\ProductSku;
use CartSave;
class CartController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index(Request $request)
    {
        $cartItems = $request->user()->cartItems()->with(['productSku.product'])->get();
        $addresses = $request->user()->addresses()->orderBy('last_used_at', 'desc')->get();

        return view('goods.cart.index', ['cartItems' => $cartItems, 'addresses' => $addresses]);
    }
    public function add(AddCartRequest $request)
    {
        CartSave::add($request);
        return [];
    }
    public function remove(ProductSku $sku, Request $request)
    {
        $request->user()->cartItems()->where('product_sku_id', $sku->id)->delete();
        return [];
    }
}
